package com.ccp.dev.qbdms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.api.ISysOrg;
import com.ccp.dev.core.basic.api.ISysUser;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.poi.excel.editor.IFontEditor;
import com.ccp.dev.core.poi.excel.reader.DataEntity;
import com.ccp.dev.core.poi.excel.reader.ExcelReader;
import com.ccp.dev.core.poi.excel.reader.FieldEntity;
import com.ccp.dev.core.poi.excel.reader.TableEntity;
import com.ccp.dev.core.poi.excel.style.Color;
import com.ccp.dev.core.poi.excel.style.DataFormat;
import com.ccp.dev.core.poi.excel.style.font.BoldWeight;
import com.ccp.dev.core.poi.excel.style.font.Font;
import com.ccp.dev.core.poi.excel.util.ExcelUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.DateFormatUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.extend.service.ExtendSysUserService;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.enums.FlowStatusEnum;
import com.ccp.dev.qbdms.expimp.CustomExcelReader;
import com.ccp.dev.qbdms.expimp.util.AbstractExpUtil;
import com.ccp.dev.qbdms.expimp.util.AbstractImpUtil;
import com.ccp.dev.qbdms.expimp.util.AuxiliaryUtil;
import com.ccp.dev.qbdms.model.WQbdDailyInspect;
import com.ccp.dev.qbdms.model.WQbdQcGroup;
import com.ccp.dev.qbdms.model.WQbdTecBasicmanage;
import com.ccp.dev.qbdms.service.WQbdFalSuperviseService;
import com.ccp.dev.qbdms.service.WQbdQcGroupService;
import com.ccp.dev.qbdms.service.WQbdTecBasicmanageService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.StructureModifyUtil;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.SysOrgService;
import com.ccp.dev.system.service.SysUserService;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.TaskOpinion;
import com.ccp.dev.workflow.model.bpm.ProcessCmd;
import com.ccp.dev.workflow.service.ProcessRunService;
import com.ccp.dev.workflow.service.TaskOpinionService;
import com.google.common.collect.Maps;
import net.sf.json.JSONObject;
import org.apache.catalina.connector.Request;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * QC小组注册 信息操作处理控制器
 *
 * @author zhl
 * @date 2020-06-22 17:31:31
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "QC小组注册")
@RequestMapping("/qbdms/wQbdQcGroup")
public class WQbdQcGroupController extends SysBaseController {
    @Resource
    private WQbdQcGroupService wQbdQcGroupService;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private ExtendSysOrgService extendSysOrgService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private WQbdFalSuperviseService wQbdFalSuperviseService;
    @Resource
    private ExtendSysUserService extendSysUserService;
    @Resource
    private WQbdTecBasicmanageService wQbdTecBasicmanageService;
    @Resource
    private ProcessRunService processRunService;
    @Resource
    private TaskOpinionService taskOpinionService;


    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "跳转到QC小组注册首页", exectype = "页面跳转日志")
    public String viewList(HttpServletRequest request, ModelMap modelMap) {
        BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
        // 返回当前系统等级
        modelMap.put("currentSystemLevel", currentSystemLevel);

        // 返回当前登录人
        modelMap.put("userId", ContextUtil.getCurrentUserId());
        SysRole sysRole = (SysRole) request.getSession().getAttribute(SysRole.CURRENT_SYSROLE);
        if (sysRole.getAlias().equals("bpm_zlzhzly") || sysRole.getAlias().equals("bpm_qnzl")) {
            modelMap.put("canEdit", "true");
        } else {
            modelMap.put("canEdit", "false");
        }

        // 只能看到下级单位,以及当前单位
        ISysOrg currentOrg = ContextUtil.getCurrentOrg();
        HashMap<String, String> hashMap = new HashMap<>(16);
        List<Map> org;
        if (BaseUtil.SystemLevel.GROUPLEVEL.equals(currentSystemLevel)) {
            // 集团看所有
            // 需要参数的时候直接放入hashMap
            hashMap.put("current", "0");
            org = wQbdFalSuperviseService.getOrg2(hashMap);
        } else {
            // 二级，三级单位看本单位以及全部三级单位
            hashMap.put("ORGSUPID", currentOrg.getOrgId());
            hashMap.put("ISTENANT", "1");
            hashMap.put("this", "this");
            org = wQbdFalSuperviseService.getOrg(hashMap);
        }

        modelMap.put("rolealis", sysRole.getAlias());
        modelMap.put("org", JSON.toJSONString(org));


        return "wQbdQcGroup/list";
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "附带检索条件的分页查询QC小组注册", detail = "查询数据信息<#if success>成功<#else></#if>")
    public Object list(HttpServletRequest request) throws IllegalAccessException {
        QueryFilter queryFilter = new QueryFilter(request, true);
        // 默认按照评定时间倒序排列,评定时间相同的情况下按创建时间倒序排列
        if (StringUtil.isEmpty(queryFilter.get("orderField"))) {
            queryFilter.addFilter("orderField", "REGISTERDATE");
            queryFilter.addFilter("orderSeq", "desc");
            queryFilter.addFilter("orderField1", "createtime");
            queryFilter.addFilter("orderSeq1", "desc");
        }
        ZhiLiangConst.addQueryFilter(queryFilter);
        SysRole sysRole = (SysRole) request.getSession().getAttribute(SysRole.CURRENT_SYSROLE);

        //分页数据
        List<WQbdQcGroup> pageDatas = wQbdQcGroupService.queryList(queryFilter);
        // 密级、上报状态、数据来源单位显示中文
        ZhiLiangConst.showName(pageDatas);
        //判断流程状态
        ZhiLiangConst.isDataAllowEditByFlowStatus(pageDatas);

        SysAuditThreadLocalHolder.putParamerter("success", true);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }


    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description = "跳转到QC小组注册新增页面", exectype = "页面跳转日志")
    public String viewAdd(HttpServletRequest request, ModelMap modelMap) {
        String flag = RequestUtil.getString(request, "flag");
        modelMap.put("flag", flag);
        String id = RequestUtil.getString(request, "id");
        modelMap.put("id", id);
        ISysUser currentUser = ContextUtil.getCurrentUser();
        String userId = currentUser.getUserId();
        SysOrg currentTenOrg = extendSysOrgService.getTenOrg(userId);
        if (StringUtil.isEmpty(id)) {
            modelMap.put("filldept", currentTenOrg.getOrgName());
            modelMap.put("filldeptid", currentTenOrg.getCode());
        }
        String runId="";
        List<TaskOpinion> taskOpinionNewList = new ArrayList<TaskOpinion>();
        ProcessRun processRun=processRunService.getByBusinessKey(id);
        if(BeanUtils.isNotEmpty(processRun)){
            runId=processRun.getRunId();
            List<TaskOpinion> taskOpinionList = taskOpinionService.getByRunId(runId);
            for(TaskOpinion taskOpinion:taskOpinionList){
                if(taskOpinion.getEndTime()!=null){
                    taskOpinionNewList.add(taskOpinion);
                }
            }
        }
        modelMap.put("taskOpinionNewList", taskOpinionNewList);
        SysRole sysRole = (SysRole) request.getSession().getAttribute(SysRole.CURRENT_SYSROLE);
        modelMap.put("roleAlias", sysRole.getAlias());
        if (StringUtils.isNotBlank(id)) {
            WQbdQcGroup wQbdQcGroup = wQbdQcGroupService.getOneById(id);
            String flowStatus=wQbdQcGroup.getFlowStatus();
            modelMap.put("flowStatus", flowStatus);
            if (flowStatus.equals("3")&&flag.equals("1")) {
                //编号
                modelMap.put("num", wQbdQcGroupService.generateNewQCGroupRegisternum());
            }
        }

        return "wQbdQcGroup/add";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: wQbdQcGroup 保存的对象
     * @return: java.lang.Object
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description = "保存QC小组注册数据", detail = "<#if isAdd>新增<#else>编辑</#if>QC小组注册数据<#if success>成功<#else>失败</#if><#if StringUtil.isNotEmpty(modifyInfo)>${modifyInfo}</#if>")
    public Object save(@RequestBody WQbdQcGroup wQbdQcGroup) {
        String id = wQbdQcGroup.getId();
        int result;
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("isAdd", true);
            //新增操作
            //新增数据前生成随机Id
            wQbdQcGroup.setId(ProjectUUIDUtils.getUUIDFor32());
            ZhiLiangConst.addCommonFields(wQbdQcGroup);
            //上报状态
            BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
            if (BaseUtil.SystemLevel.HOSPITALLEVEL.equals(currentSystemLevel)) {
                wQbdQcGroup.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            } else if (BaseUtil.SystemLevel.PLANTLEVEL.equals(currentSystemLevel)) {
                wQbdQcGroup.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            }

            wQbdQcGroup.setFlowStatus(FlowStatusEnum.NOT_SUBMIT.getValue());
            result = wQbdQcGroupService.add(wQbdQcGroup);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            ZhiLiangConst.updateCommonFields(wQbdQcGroup);

            //修改操作
            result = wQbdQcGroupService.update(wQbdQcGroup);
            SysAuditThreadLocalHolder.putParamerter("modifyInfo", StructureModifyUtil.getModifyInfo(wQbdQcGroupService.getOneById(id), wQbdQcGroup));

        }
        if (result > 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        Map<String,Object> idMap= Maps.newHashMap();
        idMap.put("id",wQbdQcGroup.getId());
        return result > 0 ? getResultDataSuccess("save.success",idMap) : getResultDataError("save.failure");
    }

    /**
     * 功能描述: 跳转到详情页面
     *
     * @return: java.lang.String
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/particulars")
    @Action(description = "跳转到QC小组注册详情页面", exectype = "页面跳转日志")
    public String viewParticulars(HttpServletRequest request, ModelMap modelMap) {
        String flag = RequestUtil.getString(request, "flag");
        modelMap.put("flag", flag);
        String id = RequestUtil.getString(request, "id");
        String isClose = RequestUtil.getString(request, "isClose");
        modelMap.put("isClose", isClose);
        modelMap.put("id", id);
        WQbdQcGroup wQbdQcGroup = wQbdQcGroupService.getOneById(id);
        modelMap.put("flowStatus", wQbdQcGroup.getFlowStatus());
        String runId="";
        List<TaskOpinion> taskOpinionNewList = new ArrayList<TaskOpinion>();
        ProcessRun processRun=processRunService.getByBusinessKey(id);
        if(BeanUtils.isNotEmpty(processRun)){
            runId=processRun.getRunId();
            List<TaskOpinion> taskOpinionList = taskOpinionService.getByRunId(runId);
            for(TaskOpinion taskOpinion:taskOpinionList){
                if(taskOpinion.getEndTime()!=null){
                    taskOpinionNewList.add(taskOpinion);
                }
            }
        }
        modelMap.put("taskOpinionNewList", taskOpinionNewList);
        modelMap.put("runId", runId);
        return "wQbdQcGroup/particulars";
    }

    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"id"
     * @return: java.lang.Object
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description = "根据主键id查询QC小组注册的一条数据", detail = "根据主键id查询QC小组注册的数据<#if success>成功<#else>失败</#if>")
    public Object get(@RequestParam("id") String id) {
        WQbdQcGroup wQbdQcGroup = wQbdQcGroupService.getOneById(id);

        if (null == wQbdQcGroup) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("get.failure");
        }
        FlowStatusEnum flowStatusEnum = FlowStatusEnum.getEnumByValue(wQbdQcGroup.getFlowStatus());
        wQbdQcGroup.setFlowStatusStr(flowStatusEnum.getName());
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("get.success", wQbdQcGroup);
    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"id"
     * @return: java.lang.Object
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description = "根据主键ID删除QC小组注册数据", exectype = "删除日志", detail = "删除数据信息<#if success>成功<#else>失败</#if>")
    public Object delete(@RequestParam("id") String id) throws Exception{
        String userId = ContextUtil.getCurrentUserId();
        String[] ids = id.split(",");
        for (String key : ids) {
            WQbdQcGroup wQbdQcGroup = wQbdQcGroupService.getOneById(key);

            //判断流程状态
            FlowStatusEnum flowStatusEnum = FlowStatusEnum.getEnumByValue(wQbdQcGroup.getFlowStatus());
            if (!flowStatusEnum.isDel()) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return fail("删除失败，不允许删除流程未结束数据！");
            }
            if (!userId.equals(wQbdQcGroup.getCreateBy())) {
                return fail("删除失败！只能删除自己创建的数据！");
            }
        }

        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("delete.failure.message");
        }
        //流程实例删除
        for (String key : ids) {
            WQbdQcGroup wQbdQcGroup  = wQbdQcGroupService.getOneById(key);
            if(!wQbdQcGroup.getFlowStatus().equals(FlowStatusEnum.NOT_SUBMIT.getValue()) && StringUtil.isNotEmpty(wQbdQcGroup.getFlowStatus())){
                ProcessRun processRun = processRunService.getByBusinessKey(key);
                if(processRun!=null){
                    processRunService.deleteByProcessRun(processRun);
                }
            }
        }
        int result = wQbdQcGroupService.delByIdArray(id.split(","));
        if (result > 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }

    /**
     * 功能描述: 跳转到导入页面
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-12-25 19:21:40
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/import")
    @Action(description = "跳转到QC小组注册导入页面", exectype = "页面跳转日志")
    public String viewImport() {
        return "wQbdQcGroup/import";
    }

    /**
     * 功能描述: 导出QC小组注册数据信息
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: zhl
     * @date: 2020-06-22 17:31:31
     */
    @RequestMapping("/exportData")
    @Action(description = "导出QC小组注册数据信息", detail = "导出QC小组注册数据信息<#if success>成功<#else></#if>")
    public void exportData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysAuditThreadLocalHolder.putParamerter("success", false);
        String[] id = RequestUtil.getStringAryByStr(request, "id");
        QueryFilter queryFilter = new QueryFilter(request, false);
        // 默认按照注册时间倒序排列,评定时间相同的情况下按创建时间倒序排列
        if (StringUtil.isEmpty(queryFilter.get("orderField"))) {
            queryFilter.addFilter("orderField", "REGISTERDATE");
            queryFilter.addFilter("orderSeq", "desc");
            queryFilter.addFilter("orderField1", "createtime");
            queryFilter.addFilter("orderSeq1", "desc");
        }
        JSONArray dataArray = ZhiLiangConst.exportAddConditions(queryFilter,id);
        List<WQbdQcGroup> wQbdQcGroups = this.wQbdQcGroupService.queryAll(queryFilter);
        if (wQbdQcGroups.isEmpty()) {
            wQbdQcGroups.add(new WQbdQcGroup());
        }
        new AbstractExpUtil() {
            @Override
            public String customAnalysis(String fieldName, String fieldValue) {
                return null;
            }
        }.executeExport(request, response, wQbdQcGroups, dataArray, AbstractExpUtil.ExpStyle.MANY_SHEET);
        SysAuditThreadLocalHolder.putParamerter("success", true);
    }



    /**
     * 功能描述: 跳转到导出选择字段页面
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-12-25 19:21:40
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/export")
    @Action(description = "跳转到QC小组导出选择字段页面", exectype = "页面跳转日志")
    public String export(HttpServletRequest request, ModelMap modelMap) throws Exception {
        com.alibaba.fastjson.JSONObject jsonObject = AuxiliaryUtil.resolverModel(WQbdQcGroup.class);
        modelMap.put("tableInfo", jsonObject.toString());
        return "expImp/add";
    }



    /**
     * 导入资源文件
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/importData")
    @ResponseBody
    @Action(description = "导入QC小组", detail = "导入QC小组<#if success>成功<#else></#if>")
    public ResultData importData(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
        SysAuditThreadLocalHolder.putParamerter("success", false);
        MultipartFile multipartFile = request.getFile("file");
        TableEntity excelEntity = new CustomExcelReader().readFile(multipartFile.getInputStream());
        ArrayList<String> strPeople = new ArrayList<>();
        strPeople.add("groupleaderid");
        strPeople.add("linkmanid");
        // 取出当前系统中所有的课题信息数据
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        HashMap<String, String> hashMap = new HashMap<>(8);
        hashMap.put("secret",sysUser.getPrivacyLevel().toString());
        List<WQbdTecBasicmanage> wQbdTecBasicmanages = wQbdTecBasicmanageService.getAll(hashMap);
        ArrayList<String> basicName = new ArrayList<>();
        wQbdTecBasicmanages.forEach(item->{
            basicName.add(item.getfItemname());
        });
        Map<String, Object> map = new AbstractImpUtil() {
            @Override
            public ReturnType customValidation(String fieldName, String fieldValue) {
                ReturnType returnType = null;
//                if ("registerdeptid".equals(fieldName)) {
//                    returnType = new ReturnType();
//                    List<SysOrg> sysOrgs = sysOrgService.queryByOrgName(fieldValue);
//                    if (sysOrgs.size() > 0) {
//                        SysOrg sysOrg = sysOrgs.get(0);
//                        if (StringUtil.isNotEmpty(sysOrg.getCode())) {
//                            returnType.setNewValue(sysOrg.getCode());
//                        } else {
//                            returnType.setNewValue("");
//                        }
//                    } else {
//                        returnType.setMsg("不存在此组织！");
//                    }
//                }

                if(strPeople.contains(fieldName)){
                    returnType = new ReturnType();
                    List<SysUser> sysUsers = extendSysUserService.getByFullname(fieldValue);
                    if (sysUsers.size() > 0) {
                        SysUser sysUser = sysUsers.get(0);
                        if (StringUtil.isNotEmpty(sysUser.getUserId())) {
                            returnType.setNewValue(sysUser.getUserId());
                        } else {
                            returnType.setMsg("不存在的人员！");
                        }
                    } else {
                        returnType.setMsg("不存在的人员！");
                    }
                }

                if("groupmembersid".equals(fieldName)){
                    returnType = new ReturnType();
                    String all = fieldValue.replaceAll("，", ",");
                    StringBuilder ids = new StringBuilder();
                    String[] split = all.split(",");
                    for (int i = 0; i < split.length; i++) {
                        List<SysUser> sysUsers = extendSysUserService.getByFullname(split[i]);
                        if (sysUsers.size() > 0) {
                            SysUser sysUser = sysUsers.get(0);
                            if (StringUtil.isNotEmpty(sysUser.getUserId())) {
                                ids.append(sysUser.getUserId());
                                if(split.length-1 > i){
                                    ids.append(",");
                                }
                            } else {
                                returnType.setMsg("不存在的人员！");
                            }
                        } else {
                            returnType.setMsg("不存在的人员！");
                        }
                    }
                    returnType.setNewValue(ids.toString());
                }

                if("groupMembers".equals(fieldName)){
                    // 如果有 中文逗号 全部替换为 英文逗号
                    returnType = new ReturnType();
                    if (fieldValue.length()>0){
                        returnType.setNewValue(fieldValue.replaceAll("，",","));
                    }else {
                        returnType.setNewValue("");
                    }
                }

                if("subjectnameid".equals(fieldName)){
                    returnType = new ReturnType();
                    if(basicName.contains(fieldValue)){
                        returnType.setNewValue("");
                    } else {
                        returnType.setMsg("输入的课题名称不存在");
                    }
                }

                // 校验位置
                return returnType;
            }
        }.reverseModels(excelEntity, WQbdQcGroup.class);
        String msg = map.get("msg").toString();

        List<WQbdQcGroup> list = (List<WQbdQcGroup>) map.get("instList");

        if (StringUtil.isEmpty(msg)) {
            this.wQbdQcGroupService.addImportData(list);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("dataSource.importXml.success");
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail(msg);
        }

    }


    /**
     * 查询上报的信息
     *
     * @param request
     * @param id
     * @param tableName
     * @return
     */
    @RequestMapping("/reportList")
    @ResponseBody
    @Action(description = "查询上报的信息")
    public Object reportList(HttpServletRequest request, @RequestParam("id") String id, @RequestParam("tableName") String tableName) throws IllegalAccessException {
        //业务数据
        List<WQbdQcGroup> reportList = wQbdQcGroupService.reportList(id, tableName);
        ZhiLiangConst.showName(reportList);
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("options.success", reportList);
    }

    /**
     * 功能描述: 启动流程
     */
    @PostMapping("/run")
    @ResponseBody
    @Action(description="启动QC小组注册流程",detail="启动流程<#if success>成功<#else></#if>")
    public Object run(HttpServletRequest request,@RequestBody WQbdQcGroup wQbdQcGroup){
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        //实体主键
        String id = wQbdQcGroup.getId();
        int isList = RequestUtil.getInt(request,"isList",1);
        ProcessCmd processCmd = new ProcessCmd();
        //流程Key
        processCmd.setModelKey("QCxzzcsplc");
        processCmd.setUserAccount(sysUser.getAccount());
        try {
            if(isList == 1){
                wQbdQcGroup = wQbdQcGroupService.getOneById(id);
            }
            processCmd.setBusinessKey(id);
            processRunService.startProcess(processCmd);
            wQbdQcGroup.setFlowStatus(FlowStatusEnum.APPROVAL_ING2.getValue());
            wQbdQcGroupService.update(wQbdQcGroup);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            return getResultDataSuccess("flow.start.success");
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("flow.start.failure");
        }
    }
}
